<template>
  <div class="privacy-container">
    <!-- 页面头部 -->
    <div class="privacy-header">
      <div class="header-content">
        <div class="header-title">
          <i class="el-icon-document-copy" />
          <h2>隐私条款管理</h2>
        </div>
        <p class="header-description">管理系统的隐私条款内容，支持上传.md格式文件</p>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="privacy-main">
      <!-- 上传区域 -->
      <el-card class="upload-card" shadow="hover">
        <div slot="header" class="card-header">
          <div class="card-title">
            <i class="el-icon-upload2" />
            <span>上传隐私条款</span>
          </div>
          <el-button
            type="primary"
            size="small"
            :loading="loading"
            icon="el-icon-refresh"
            @click="refreshContent"
          >
            刷新内容
          </el-button>
        </div>

        <el-upload
          ref="upload"
          class="upload-dragger"
          drag
          :action="uploadUrl"
          :headers="uploadHeaders"
          :data="uploadData"
          :before-upload="beforeUpload"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
          :show-file-list="false"
          accept=".md"
        >
          <div class="upload-content">
            <i class="el-icon-upload upload-icon" />
            <div class="upload-text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="upload-tip">
              只能上传.md文件，且不超过10MB
            </div>
          </div>
        </el-upload>
      </el-card>

      <!-- 内容显示区域 -->
      <el-card class="content-card" shadow="hover">
        <div slot="header" class="card-header">
          <div class="card-title">
            <i class="el-icon-document" />
            <span>当前隐私条款内容</span>
          </div>
          <el-button
            type="success"
            size="small"
            :disabled="!privacyContent"
            icon="el-icon-download"
            @click="downloadContent"
          >
            下载内容
          </el-button>
        </div>

        <div class="content-display">
          <div v-if="loading" v-loading="loading" class="loading-container" element-loading-text="加载中..." />
          <div v-else-if="privacyContent" class="markdown-content">
            <div class="content-wrapper">
              <pre><code>{{ privacyContent }}</code></pre>
            </div>
          </div>
          <div v-else class="empty-content">
            <div class="empty-icon">
              <i class="el-icon-document" />
            </div>
            <p class="empty-text">暂无隐私条款内容</p>
            <p class="empty-hint">请上传.md格式的隐私条款文件</p>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import documentMixin from '@/mixins/documentMixin'

export default {
  name: 'Privacy',
  mixins: [documentMixin],
  data() {
    return {
      uploadUrl: '/dev-api/management/system/text/updatePrivacyClause',
      uploadHeaders: {
        'Authorization': 'Bearer ' + this.$store.getters.token
      },
      uploadData: {}
    }
  },
  created() {
    this.loadPrivacyContent()
  },
  methods: {
    // 上传前验证
    beforeUpload(file) {
      return this.beforePrivacyUpload(file)
    },

    // 上传成功
    handleUploadSuccess(response, file) {
      this.handlePrivacyUploadSuccess(response, file)
    },

    // 上传失败
    handleUploadError(error) {
      this.handlePrivacyUploadError(error)
    },

    // 刷新内容
    refreshContent() {
      this.refreshPrivacyContent()
    },

    // 下载内容
    downloadContent() {
      this.downloadPrivacyContent()
    }
  }
}
</script>

<style lang="scss" scoped>
.privacy-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;

  @media (max-width: 768px) {
    padding: 10px;
  }
}

.privacy-header {
  margin-bottom: 24px;

  .header-content {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(0, 0, 0, 0.05);

    .header-title {
      display: flex;
      align-items: center;
      margin-bottom: 8px;

      i {
        font-size: 24px;
        color: #409eff;
        margin-right: 12px;
      }

      h2 {
        margin: 0;
        font-size: 24px;
        font-weight: 600;
        color: #303133;
        letter-spacing: 0.5px;
      }
    }

    .header-description {
      margin: 0;
      color: #606266;
      font-size: 14px;
      line-height: 1.6;
    }
  }
}

.privacy-main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 24px;

  @media (min-width: 1200px) {
    grid-template-columns: 1fr 1fr;
  }
}

.upload-card,
.content-card {
  border-radius: 12px;
  border: none;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  }

  ::v-deep .el-card__header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 12px 12px 0 0;
    padding: 16px 20px;
    border: none;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .card-title {
        display: flex;
        align-items: center;

        i {
          font-size: 18px;
          margin-right: 8px;
        }

        span {
          font-size: 16px;
          font-weight: 600;
        }
      }
    }
  }

  ::v-deep .el-card__body {
    padding: 24px;
  }
}

.upload-dragger {
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  background: #fafafa;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
  width: 100%;

  &:hover {
    border-color: #409eff;
    background: #f0f9ff;
  }

  ::v-deep .el-upload-dragger {
    width: 100%;
    height: 100%;
    min-height: 300px;
    border: none;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;

    .upload-content {
      text-align: center;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      padding: 20px;

      .upload-icon {
        font-size: 64px;
        color: #c0c4cc;
        margin-bottom: 20px;
        display: block;
        transition: all 0.3s ease;
      }

      .upload-text {
        font-size: 18px;
        color: #606266;
        margin-bottom: 12px;
        line-height: 1.5;

        em {
          color: #409eff;
          font-style: normal;
          font-weight: 500;
        }
      }

      .upload-tip {
        font-size: 14px;
        color: #909399;
        line-height: 1.4;
      }
    }
  }

  &:hover ::v-deep .el-upload-dragger .upload-content .upload-icon {
    color: #409eff;
    transform: scale(1.1);
  }
}

.content-display {
  min-height: 300px;

  .loading-container {
    height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .markdown-content {
    .content-wrapper {
      background: #f8f9fa;
      border-radius: 8px;
      padding: 16px;
      border: 1px solid #e9ecef;
      max-height: 400px;
      overflow-y: auto;

      pre {
        margin: 0;
        white-space: pre-wrap;
        word-wrap: break-word;
        font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
        font-size: 13px;
        line-height: 1.6;
        color: #495057;

        code {
          background: none;
          padding: 0;
          color: inherit;
          font-size: inherit;
        }
      }
    }
  }

  .empty-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;
    text-align: center;

    .empty-icon {
      margin-bottom: 16px;

      i {
        font-size: 64px;
        color: #c0c4cc;
      }
    }

    .empty-text {
      font-size: 16px;
      color: #909399;
      margin: 0 0 8px 0;
      font-weight: 500;
    }

    .empty-hint {
      font-size: 14px;
      color: #c0c4cc;
      margin: 0;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .privacy-header .header-content {
    padding: 16px;

    .header-title h2 {
      font-size: 20px;
    }
  }

  .upload-card,
  .content-card {
    ::v-deep .el-card__body {
      padding: 16px;
    }
  }

  .upload-dragger {
    min-height: 200px;

    ::v-deep .el-upload-dragger {
      min-height: 200px;

      .upload-content {
        padding: 16px;

        .upload-icon {
          font-size: 48px;
          margin-bottom: 16px;
        }

        .upload-text {
          font-size: 16px;
        }

        .upload-tip {
          font-size: 12px;
        }
      }
    }
  }

  .content-display {
    min-height: 250px;

    .markdown-content .content-wrapper {
      max-height: 300px;
      padding: 12px;

      pre {
        font-size: 12px;
      }
    }

    .empty-content {
      height: 250px;

      .empty-icon i {
        font-size: 48px;
      }

      .empty-text {
        font-size: 14px;
      }

      .empty-hint {
        font-size: 12px;
      }
    }
  }
}

// 动画效果
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.upload-card,
.content-card {
  animation: fadeInUp 0.6s ease-out;
}

.content-card {
  animation-delay: 0.2s;
}

// 滚动条样式
.content-display .markdown-content .content-wrapper::-webkit-scrollbar {
  width: 6px;
}

.content-display .markdown-content .content-wrapper::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.content-display .markdown-content .content-wrapper::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;

  &:hover {
    background: #a8a8a8;
  }
}
</style>

